/**
 * 推荐码管理页面管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：推荐码管理
     创建日期：2015-10-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	reportList();
	 $(".date").datepicker({
	        todayBtn: "linked",
	        keyboardNavigation: false,
	        forceParse: false,
	        calendarWeeks: true,
	        autoclose: true
	    });
});

/**
 * 查询推荐码
 * @returns
 */
var reportList = function(){

	var datas =  {"p":"reportManage","m":"findReportListPaged" };
	var columnDefine = {"id":"ID","sqlStr":"sql","remark":"备注","title":"报表名称","params":"参数设置","paramsHtml":"paramsHtml"};
	
	var formatters={
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="openModifyWin();">编辑</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showExportHtmlWin();">查询报表</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-warning\' onclick="showExportExcelWin();">导出到Excel</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="deleteReport();">删除</button>&nbsp;&nbsp;';
		buttons+='</p>';
	   hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};

/**
 * 添加参数
 */
var addParam = function( winId ){
	
	var html = '';
	html += '<div class="form-group params">';
	    html+='<label class="col-sm-2 control-label"></label>';
		html += '<div class="col-sm-3">';
			html += '<input type="text" class="form-control param0"  placeholder="sql中对应的参数名称">';
		html += '</div>';
		html += '<div class="col-sm-3">';
			html += '<input type="text" class="form-control param1"  placeholder="参数名称对应中文名称">';
		html += '</div>';
		html += '<div class="col-sm-2">';
			html += '<button type="button" class="btn btn-danger" onclick="deleteParam(this);">删除</button>';
		html += '</div>';
	html += '</div>	';
	$('#'+winId +' form').append(html);
};

/**
 * 删除参数
 * @param obj
 * @returns
 */
var deleteParam = function( obj ){
	
	$(obj).parent().parent().remove();
};

/**
 * 
 * @returns
 */
var openNewWin = function(){
	$('#pop_win').modal('hide');
	$('#newPop input').val('');
	$('#newPop').modal('show');
};

/**
 * 打开编辑窗口
 * @returns
 */
var openModifyWin = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	
	$('#modifyPop .id').val(json.id);
	$('#modifyPop .title').val(json.title);
	$('#modifyPop .sql').val(json.sqlStr);
	$('#modifyPop .remark').val( json.remark);
	var params = json.params;
	
	$('#modifyPop .params').remove();
	
	var html='';
	if( null!=params && params!='' )
	{
		
		if( params.indexOf(':')>0 )
		{
			  var array = params.split(';');
			  
			  if( array.length > 0 )
			  {
				  for( var i = 0 ; i < array.length; i++ )
				  {
					  var param = array[i].split(':');
					  html += '<div class="form-group params">';
					    html+='<label class="col-sm-2 control-label"></label>';
						html += '<div class="col-sm-3">';
							html += '<input type="text" class="form-control param0"  placeholder="sql中对应的参数名称" value="'+param[0]+'">';
						html += '</div>';
						html += '<div class="col-sm-3">';
							html += '<input type="text" class="form-control param1"  placeholder="参数名称对应中文名称" value="'+param[1]+'">';
						html += '</div>';
						html += '<div class="col-sm-2">';
							html += '<button type="button" class="btn btn-danger" onclick="deleteParam(this);">删除</button>';
						html += '</div>';
				     html += '</div>	';
				  }	  
			  }	  
		}	
	}	
	
	$('#modifyPop form').append(html);
	
	$('#pop_win').modal('hide');
	$('#modifyPop').modal('show');
	
};

/**
 * 新建报表
 * @returns
 */
var addReport = function(){
	
	var title = $('#newPop .title').val();
	var sql = $('#newPop .sql').val();
	var remark = $('#newPop .remark').val();
	var params = '';
	var param0  = $('#newPop .param0');
	var param1  = $('#newPop .param1');
	if( param0.length > 0 )
	{
		for( var  i = 0 ; i <= param0.length ; i++ )
		{
			var value0 = $(param0[i]).val();
			var value1 = $(param1[i]).val();
			if( null!=value0 && null!=value1 )
			{
				params += value0;
				params += ':';
				params += value1;
				params += ';';
			}	
		}	
	}	
	if( params.indexOf(';')>0 )
	{
		params = params.substring(0, params.lastIndexOf(';')-1);
	}	
	var datas = {"p":"reportManage","m":"addReport","title":title,"sql":sql,"remark":remark,"params":params};
	var json = common.ajaxRequest('service', datas, 'POST');
	if( json && json.result == 0 )
	{
		reportList();
		$('#newPop').modal('hide');
		common.showMsg('info','','新建成功!');
	}
	else
	{
		common.showMsg('error','','新建失败,'+json.message);
	}	
	
};

/**
 * 修改报表
 * @returns
 */
var modifyReport = function(){
	var id = $('#modifyPop .id').val();
	var title = $('#modifyPop .title').val();
	var sql = $('#modifyPop .sql').val();
	var remark = $('#modifyPop .remark').val();
	var params = '';
	var param0  = $('#modifyPop .param0');
	var param1  = $('#modifyPop .param1');
	if( param0.length > 0 )
	{
		for( var  i = 0 ; i <= param0.length ; i++ )
		{
			var value0 = $(param0[i]).val();
			var value1 = $(param1[i]).val();
			
			if( null!=value0 && null!=value1 )
			{
				params += value0;
				params += ':';
				params += value1;
				params += ';';
			}	
		}	
	}	
	if( params.indexOf(';')>0 )
	{
		params = params.substring(0, params.lastIndexOf(';'));
	}	
	var datas = {"p":"reportManage","m":"modifyReport","title":title,"sql":sql,"remark":remark,"params":params,"id":id};
	var json = common.ajaxRequest('service', datas, 'POST');
	if( json && json.result == 0 )
	{
		reportList();
		$('#modifyPop').modal('hide');
		common.showMsg('info','','修改成功!');
	}
	else
	{
		common.showMsg('error','','修改失败,'+json.message);
	}	
};


/**
 * 删除报表
 * @returns
 */
var deleteReport = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	var datas = {"p":"reportManage","m":"deleteReport","id":json.id};
	var json = common.ajaxRequest('service', datas, 'POST');
	if( json && json.result == 0 )
	{
		reportList();
		$('#pop_win').modal('hide');
		common.showMsg('info','','删除成功!');
	}
	else
	{
		common.showMsg('error','','删除失败,'+json.message);
	}	
};


/**
 * 显示导出HTML数据窗口
 * @returns
 */
var showExportHtmlWin = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#exportHtmlWin .modal-title').text(json.title);
	$('#exportHtmlWin .id').val(json.id);
	var params = json.params;
	$('#exportHtmlWin .params').remove();
	var html='';
	if( null!=params && params!='' )
	{
		
		if( params.indexOf(':')>0 )
		{
			  var array = params.split(';');
			  
			  if( array.length > 0 )
			  {
				  for( var i = 0 ; i < array.length; i++ )
				  {
					  var param = array[i].split(':');
					  
					  html += '<div class="form-group params">';
						  html += '<label class="col-sm-2 control-label">'+param[1]+'</label>';
						  html += '<div class="col-sm-8">';
						  	html += '<input type="text" class="form-control " paramName="'+param[0]+'" placeholder="'+param[1]+'">';
						  html += '</div>';
					  html += '</div>	';
				  }	  
			  }	  
		}	
	}	
	
	$('#exportHtmlWin form').append(html);
	$('#pop_win').modal('hide');
	$('#exportHtmlWin').modal('show');
};



/**
 * 导出Html
 * @returns
 */
var exportHtml = function(){
	
	var inputs = $('#exportHtmlWin input');
	var dataBase = $('#exportHtmlWin .dataBase').val();
	var params = 'p=exportReportToHtml&m=exportDataToHtml&dataBase='+dataBase;
	if( inputs.length > 0 )
	{
		$(inputs).each(function(){
			params+=  '&' + $(this).attr('paramName')+'='+$(this).val();
		});
	}	
	common.showProgress();
	var json = common.ajaxRequest('jsp/config.jsp', null, 'GET');
	var url = json.url;
	url = url.replace('ajax', 'html');
	//var json = common.ajaxRequest('service?' + params , null , 'GET');
	window.open( url + '?mobile=true&' +params);
	common.hideProgress();
/*	if( json && json.result == 0 )
	{
		//common.showMsg('info','','导出成功!');
		//window.open(json.excelFile);
	}
	else
	{
		common.showMsg('error','','导出失败,'+json.message);
	}*/	
};

/**
 * 显示导出excel数据操作窗口
 * @returns
 */
var showExportExcelWin = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#exportExcelWin .modal-title').text('导出报表《'+json.title+'》');
	$('#exportExcelWin .id').val(json.id);
	var params = json.params;
	$('#exportExcelWin .params').remove();
	var html='';
	if( null!=params && params!='' )
	{
		
		if( params.indexOf(':')>0 )
		{
			  var array = params.split(';');
			  
			  if( array.length > 0 )
			  {
				  for( var i = 0 ; i < array.length; i++ )
				  {
					  var param = array[i].split(':');
					  
					  html += '<div class="form-group params">';
						  html += '<label class="col-sm-2 control-label">'+param[1]+'</label>';
						  html += '<div class="col-sm-8">';
						  	html += '<input type="text" class="form-control " paramName="'+param[0]+'" placeholder="'+param[1]+'">';
						  html += '</div>';
					  html += '</div>	';
				  }	  
			  }	  
		}	
	}	
	
	$('#exportExcelWin form').append(html);
	$('#pop_win').modal('hide');
	$('#exportExcelWin').modal('show');
};

/**
 * 导出Excel
 * @returns
 */
var exportExcel = function(){
	
	var inputs = $('#exportExcelWin input');
	var dataBase = $('#exportExcelWin .dataBase').val();
	var params = 'p=reportManage&m=exportDataToExcel&dataBase='+dataBase;
	if( inputs.length > 0 )
	{
		$(inputs).each(function(){
			params+=  '&' + $(this).attr('paramName')+'='+$(this).val();
		});
	}	
	common.showProgress();
	var json = common.ajaxRequest('service?' + params , null , 'GET');
	common.hideProgress();
	if( json && json.result == 0 )
	{
		$('#exportExcelWin').modal('hide');
		common.showMsg('info','','导出成功!');
		window.open(json.excelFile);
	}
	else
	{
		common.showMsg('error','','导出失败,'+json.message);
	}	
};

